package org.grottarossa.ui.core.container;


import java.io.Serializable;
import java.util.ResourceBundle;

import org.grottarossa.template.data.pojo.Menu;
import org.grottarossa.template.data.pojo.Menu.MenuType;
import org.grottarossa.ui.core.i18n.DM;

import com.vaadin.data.util.HierarchicalContainer;
import com.vaadin.terminal.Resource;

public class MenuContainer extends HierarchicalContainer implements Serializable {

	/**
	 *
	 */
	private static final long serialVersionUID = 1703222381484275093L;

	private ResourceBundle m_localizer = DM.instance().getBundle(MenuContainer.class.getName());

	private String menuName;

	public MenuContainer(Menu root, String m_menuName) {

		super();

		this.menuName = m_menuName;
		addContainerProperty("icon", Resource.class, null);
		addContainerProperty("caption", String.class, null);
		addContainerProperty("type", MenuType.class, null);
		addContainerProperty("script", String.class, null);
		addContainerProperty("description", String.class, null);
		addContainerProperty("forward", String.class, null);

		add2Container(null, root);
	}

	public void add2Container(Object objRoot, Menu root)
	{
		String localizedLabel="";

		for(int j=0;j<root.getMenus().size();j++)
		{
			Menu menu = root.getMenus().get(j);

			if(menu.getLabel()!=null)
			{
				Object obj = addItem();
				//container.getContainerProperty(objRoot, "icon").setValue(new ThemeResource("../runo/icons/16/folder.png"));

				//retrieve localized label
				try {
					if ( menu.getScriptId().equals("") )
						localizedLabel = m_localizer.getString(String.format("menu.%s.%s.caption", menuName, menu.getLabel()));
					else
						localizedLabel = m_localizer.getString(String.format("menu.%s.%s.caption", menuName, menu.getScriptId()));
				} catch(Exception ex) {
					localizedLabel = menu.getLabel();
				}

				getContainerProperty(obj, "caption").setValue(localizedLabel);
				getContainerProperty(obj, "type").setValue(menu.getType());
				getContainerProperty(obj, "description").setValue(menu.getDescription());
				getContainerProperty(obj, "forward").setValue(menu.getForward());

				if ( objRoot != null)
					setParent(obj, objRoot);

				if ( menu.isLeaf())
				{
					getContainerProperty(obj, "script").setValue(menu.getScriptId());
					setChildrenAllowed(obj,false);
				}
				else
				{
					add2Container(obj, menu);
				}
			}
		}
	}
}
